package com.cn.kabalifeuser.ui.my.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.my.bean.CashListBean;
import com.cn.kabalifeuser.ui.my.bean.ReportFormListBean;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.utils.SoftHideKeyBoardUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/9/18.
 * function: 提现
 */
public class IMoneyTiXianActivity extends IBaseActivity<IMoneyTiXianView, IMoneyTiXianPresenter> implements IMoneyTiXianView {
    @BindView(R.id.tv_money_all)
    TextView tvMoneyAll;
    @BindView(R.id.img_check_weixin)
    ImageView imgCheckWeixin;
    @BindView(R.id.img_check_zfb)
    ImageView imgCheckZfb;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.et_card)
    EditText etCard;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_content)
    EditText etContent;

    @BindView(R.id.et_card_type)
    EditText et_card_type;
    @BindView(R.id.et_card_type_x)
    TextView et_card_type_x;

    @BindView(R.id.tv_case)
    TextView tv_case;


    int paySign = 2;

    @Override
    protected IMoneyTiXianPresenter createPresenter() {
        return new IMoneyTiXianPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_cash;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.onReportFormData(LoginHelper.getLonginData().getResult());
    }

    @Override
    public void rightClick() {
        super.rightClick();
        $startActivity(IMoneyTiXianListActivity.class);
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBarRight("提现", "提现记录");
        SoftHideKeyBoardUtil.assistActivity(this);

        initPayData(2);
        etMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                LoggerUtils.d("beforeTextChanged:" + s + "start:" + start + "count:" + count + "after:" + after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                LoggerUtils.d("onTextChanged:" + s + "start:" + start + "before:" + before + "count:" + count);
//                if (etAvg.getText().toString().matches("0")) {//判断当前的输入第一个数是不是为0
//                    etAvg.setText("");
//                }
                if (s.toString().trim().substring(0).equals(".")) {
                    etMoney.setText("");
                }
                if (s.toString().trim().substring(0).equals("0")) {
                    etMoney.setText("");
                }
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        etMoney.setText(s);
                        etMoney.setSelection(s.length());
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                LoggerUtils.d("afterTextChanged:" + s.toString());
            }
        });
    }

    private void initPayData(int index) {
        switch (index) {
            case 3:
                paySign = 3;
                imgCheckWeixin.setImageResource(R.drawable.check_true);
                imgCheckZfb.setImageResource(R.drawable.check_false);
                etCard.setHint("请输入提现账号");
                etName.setHint("请输入提现姓名");
                et_card_type.setVisibility(View.VISIBLE);
                et_card_type_x.setVisibility(View.VISIBLE);
                break;
            case 2:
                paySign = 2;
                imgCheckWeixin.setImageResource(R.drawable.check_false);
                imgCheckZfb.setImageResource(R.drawable.check_true);
                etCard.setHint("请输入提现支付宝账户");
                etName.setHint("请输入提现支付宝实名");
                et_card_type.setVisibility(View.GONE);
                et_card_type_x.setVisibility(View.GONE);
                break;
        }
    }

    @OnClick({R.id.ll_btn_pay_weixin, R.id.ll_btn_pay_zfb, R.id.tv_btn_all, R.id.tv_btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_btn_pay_weixin:
                initPayData(3);
                break;
            case R.id.ll_btn_pay_zfb:
                initPayData(2);
                break;
            case R.id.tv_btn_all:
                etMoney.setText(tvMoneyAll.getText().toString().trim());
                break;
            case R.id.tv_btn_next:
                if (!TextUtils.isEmpty(etMoney.getText().toString().trim())) {
                    double money = Double.parseDouble(etMoney.getText().toString().trim());
                    if (money > 0) {
                        if (paySign == 3) {
                            //todo 銀聯
                            mPresenter.onTiXianData(LoginHelper.getLonginData().getResult(), paySign,
                                    etMoney.getText().toString().trim(),
                                    etCard.getText().toString().trim(),
                                    etName.getText().toString().trim(), et_card_type.getText().toString().trim(),
                                    etContent.getText().toString().trim());
                        } else {
                            if (!TextUtils.isEmpty(etCard.getText().toString().trim())) {
                                if (!TextUtils.isEmpty(etName.getText().toString().trim())) {
                                    mPresenter.onTiXianData(LoginHelper.getLonginData().getResult(), paySign,
                                            etMoney.getText().toString().trim(),
                                            etCard.getText().toString().trim(),
                                            etName.getText().toString().trim(), "",
                                            etContent.getText().toString().trim());
                                } else RxToast.error("请输入支付宝账户");
                            } else RxToast.error("请输入支付宝实名");
                        }

                    } else RxToast.error("提现金额必须大于0");
                } else RxToast.error("请输入提现金额");
                break;
        }
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onReportFormSuccess(ReportFormListBean bean) {
        tvMoneyAll.setText(bean.getResult().getMoney());
        tv_case.setText("提现手续费为：" + bean.getResult().getCash() + "%, 提现额度为：" + bean.getResult().getMax_cash()+"元");
    }

    @Override
    public void onTiXianSuccess(MsgBean bean) {
        RxToast.success(bean.getMsg());
        $startActivity(IMoneyTiXianSuccessActivity.class);
    }

    @Override
    public void onCashListSuccess(CashListBean bean) {

    }
}
